import { numberFormat } from '@/utils'
import Song from '@/entity/Song'
import { detail, url } from 'api/song'

class Album {
  constructor(
    {
      artist,
      picUrl,
      name,
      size,
      songs,
      info,
      description,
      publishTime,
      id
    },
  )
  {
    this.id = id // id
    this.name = name // 歌单名称
    this.artist = artist // 歌手
    this.picUrl = picUrl // 封面
    this.size = size // 歌曲数量
    this.songs = songs
    this.description = description
    this.publishTime = new Date(publishTime).format('yyyy.MM.dd')

    if (info) {
      const { commentCount, shareCount } = info
      this.commentCount = numberFormat(commentCount) // 评论数
      this.shareCount = numberFormat(shareCount) // 分享数
    }
  }
  getSongList() {
    return new Promise((resolve, reject) => {
      let ids = this.songs.map(item => item.privilege.id).join(',')

      Promise.all([detail(ids), url(ids)]).then(res => {
        let songList = res[0].songs
        let urlList = res[1].data
  
        songList = songList.map((song, index) => {
          let _song = new Song(song)
          // _song.isVip = this.privileges.includes(song.id)
          _song.url = urlList[index].url
          return _song
        })
        resolve(songList)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default Album